package com.ktjy.controller;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.http.HttpRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

//统一到该类中拿tomcat容器中的session,request....这些对象
public class BaseController {

    //重定向
    public String redirect(String path) {
        return UrlBasedViewResolver.REDIRECT_URL_PREFIX + path;
    }

    //转发
    public String forward(String path) {
        return UrlBasedViewResolver.FORWARD_URL_PREFIX + path;
    }


    public HttpServletRequest getHttpServletRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
    }

    //session
    public HttpSession getSession() {
        return getHttpServletRequest().getSession();
    }

    public void setSession(String name, Object value) {
        //获得session
        getSession().setAttribute(name, value);
    }

    public <T> T getSession(String name) {
        return (T) getSession().getAttribute(name);
    }

}
